"""web文件系统"""

import os, sys, json, math
from src.core.variables.status import SUCCESS_CODE_CUSTOMIZE, ERROR_CODE_CUSTOMIZE


def convert_size(size_bytes):
    if size_bytes == 0:
        return "0B"
    size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
    i = int(math.floor(math.log(size_bytes, 1024)))
    p = math.pow(1024, i)
    s = round(size_bytes / p, 2)
    return f"{s}{size_name[i]}"


class Wfs:
    """
    param:
        full_path: 绝对路径
    """

    def __init__(self) -> None:
        # # 分区
        # self.partitions
        # # 块设备
        # self.block_device
        # # 挂载点
        # self.mounted
        # # 文件系统
        # self.fs
        ...

    def ls(self, full_path) -> dict:
        file_list = {}
        if os.path.exists(full_path):
            if not os.path.isfile(full_path):
                for i in os.listdir(full_path):
                    type_ = (
                        "file" if os.path.isfile(os.path.join(full_path, i)) else "dir"
                    )
                    file_list[i] = {
                        "type": type_,
                        "size": (
                            "<__DIR__>"
                            if type_ == "dir"
                            else convert_size(
                                os.path.getsize(os.path.join(full_path, i))
                            )
                        ),
                        "time": os.path.getmtime(os.path.join(full_path, i)),
                    }
                return SUCCESS_CODE_CUSTOMIZE(file_list)
            file_name = os.path.basename(full_path)
            return ERROR_CODE_CUSTOMIZE(f"{file_name}是一个文件！", 400)
        return ERROR_CODE_CUSTOMIZE(f"{full_path}不存在！", 400)

    def add(self, full_path) -> dict: ...

    def remove(self, full_path) -> dict: ...

    def mkdir(self, full_path) -> dict: ...

    def copy(self, full_path) -> dict: ...

    def move(self, full_path) -> dict: ...
